import processing.core.*; import processing.xml.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class pete_wiki_map extends PApplet {

public float[] assignPoint(float x, float y, float angle, float distance) {
  float thisangle = map(angle, 0, 360, 0, TWO_PI);
  x = cos(thisangle)*distance+x;
  y = sin(thisangle)*distance+y;
  float[] returnvalues = {x,y};
  return returnvalues;
}
// settings
final int NODE_SIZE = 15;
//final String apiurl = "http://mac-mini.local/~petevilter/thanksgiving/api.php";
final String apiurl = "http://mac-mini.local/~petevilter/pete_wiki/api.php";
//final String apiurl = "api.xml";
ArrayList articles = new ArrayList();
int maxrevisions = 0;

public void setup() {
  noLoop();
  size(600,600);
  smooth();
  background(0);
}
public void draw() {
  // get data
  XMLElement db = new XMLElement(this,apiurl);
  XMLElement pages = db.getChild(0);
  // for later...
  float angle = 0;
  float angleincrement = 360/pages.getChildCount();
  // cycle through articles
  for(int i=0; i<pages.getChildCount(); i++) {
    XMLElement page = pages.getChild(i);
    // make a new object & draw it
    float[] articlepos = assignPoint(width/2,height/2,angle,height/2-60);
    Article article = new Article(articlepos,page);
    articles.add(article);
    angle += angleincrement;
  }
  // draw links
  for(int i=0; i<pages.getChildCount(); i++) {
    Article article = (Article)articles.get(i);
    article.render();
    article.drawLinks();
  }
}

class Article {
  ArrayList links = new ArrayList();
  float x;
  float y;
  String name;
  int words;
  int revisions;

  public Article(float[] position, XMLElement page) {
    name = page.getStringAttribute("title");
    for(int j=0; j<page.getChildCount(); j++) {
      links.add(page.getChild(j).getStringAttribute("to"));
    }
    revisions = page.getIntAttribute("revisions");
    if(revisions > maxrevisions) {
      maxrevisions = revisions;
    }
    words = page.getIntAttribute("words");
    x = position[0];
    y = position[1];
    
  }

  public void render() {
    float shade = 255-(map(revisions,0,maxrevisions,0,255));
    print(shade+"\n");
    fill(255,shade,shade);
    noStroke();
    float nodesize = 5+words/6; // needs tweaking
    ellipse(x,y,nodesize,nodesize);
    fill(255);
    PFont georgia = loadFont("Georgia-14.vlw");
    textFont(georgia,14);
    textAlign(CENTER);
    text(name,x,y+25);
    // should go to article when clicked on
  }
  public void drawLinks() {
    for(int i=0; i<links.size(); i++) {
      String linkname = links.get(i)+"";
      for(int j=0; j<articles.size(); j++) {
        Article article = (Article)articles.get(j);
        String articlename = article.name;
        //print("\n----\n"+articlename+"\n"+linkname+"\n"+(articlename.length()==linkname.length()));
        if(articlename.length() == linkname.length()) { // ?!?!??
          // how to indicate link direction?
          stroke(255);
          line(this.x,this.y,article.x,article.y);
        }
      }
    }
  }
}

/*

categories
- size: pages w/ in them
- squares to differentiate..

links to articles when you click 'em!

force direction
- more important (bigger, more revisions) pages draw linked pages to them




*/

  static public void main(String args[]) {     PApplet.main(new String[] { "pete_wiki_map" });  }}